package com.kurumi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kurumi.config.MyConfig;
import com.kurumi.enums.TableNameEnum;
import com.kurumi.mapper.BaseInfoMapper;
import com.kurumi.service.BaseInfoService;
import com.kurumi.util.StringUtil;

@Service
public class BaseInfoServiceImpl implements BaseInfoService {

	private String hospitalCode = "49417303-1";
	
	@Autowired
	private MyConfig myConfig;
	
	@Autowired
	private BaseInfoMapper baseInfoMapper;
	
	@Override
	public List<Map<String, Object>> getHospitalByCode(String hospitalCode) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getHospitalByCode(hospitalCode);
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfBasicInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> medicalPayMentTypes = baseInfoMapper.getMedicalPaymentTypes();
		
		List<Map<String, Object>> sexs = baseInfoMapper.getSexs();
		
		List<Map<String, Object>> marriages = baseInfoMapper.getMarriages();
		
		baseInfo.put("medicalPayMentTypes", medicalPayMentTypes);
		baseInfo.put("sexs", sexs);
		baseInfo.put("marriages", marriages);
		
		List<Map<String, Object>> idDocumentTypes = baseInfoMapper.getIdDocumentTypes();
		baseInfo.put("idDocumentTypes", idDocumentTypes);
		
		List<Map<String, Object>> nationalitys = baseInfoMapper.getNationalitys();
		baseInfo.put("nationalitys", nationalitys);
		
		List<Map<String, Object>> nations = baseInfoMapper.getNations();
		baseInfo.put("nations", nations);
		
		List<Map<String, Object>> professions = baseInfoMapper.getProfessions();
		baseInfo.put("professions", professions);
		
		List<Map<String, Object>> relativeRelations = baseInfoMapper.getRelativeRelations();
		baseInfo.put("relativeRelations", relativeRelations);
		
		List<Map<String, Object>> inHospitalTypes = baseInfoMapper.getInHospitalTypes();
		baseInfo.put("inHospitalTypes", inHospitalTypes);
		
		List<Map<String, Object>> inHospitalStates = baseInfoMapper.getInHospitalStates();
		baseInfo.put("inHospitalStates", inHospitalStates);
		
		List<Map<String, Object>> steatTypeStates = baseInfoMapper.getSteatTypeStates();
		baseInfo.put("steatTypeStates", steatTypeStates);
		
		List<Map<String, Object>> outHospitalTypes = baseInfoMapper.getOutHospitalTypes();
		baseInfo.put("outHospitalTypes", outHospitalTypes);
		
		List<Map<String, Object>> rehospitalAims = baseInfoMapper.getRehospitalAims();
		baseInfo.put("rehospitalAims", rehospitalAims);
		
		List<Map<String, Object>> hospitalDealthReasons = baseInfoMapper.getHospitalDealthReasons();
		baseInfo.put("hospitalDealthReasons", hospitalDealthReasons);
		
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		medicalDepts = baseInfoMapper.getMedicalDeptByHospitalCode(hospitalCode);
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		return baseInfo;
	}

	@Override
	public List<Map<String, Object>> getAllDiseaseByQueryName(String queryName) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> diseases = baseInfoMapper.getDiseaseByQueryName("%"+queryName+"%");
		diseases.addAll(baseInfoMapper.getMorphologyByQueryName("%"+queryName+"%"));
		diseases.addAll(baseInfoMapper.getInjuryPoisoningByQueryName("%"+queryName+"%"));
		return diseases;
	}
	
	@Override
	public List<Map<String, Object>> getDiseaseByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getDiseaseByQueryName("%"+queryName+"%");
	}
	
	@Override
	public List<Map<String, Object>> getMorphologyByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getMorphologyByQueryName("%"+queryName+"%");
	}
	
	@Override
	public List<Map<String, Object>> getInjuryPoisoningByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getInjuryPoisoningByQueryName("%"+queryName+"%");
	}


	@Override
	public List<Map<String, Object>> getMedicalWorkerByQueryName(
			String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getMedicalWorkerByQueryName("%"+queryName+"%");
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfDiagInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> diagTypes = baseInfoMapper.getDiagTypes();
		baseInfo.put("diagTypes", diagTypes);
		List<Map<String, Object>> inHospitalizationDiseaseStates = baseInfoMapper.getInHospitalizationDiseaseState();
		baseInfo.put("inHospitalizationDiseaseStates", inHospitalizationDiseaseStates);
		
		
		List<Map<String, Object>> treatmentResults = baseInfoMapper.getTreatmentResults();
		baseInfo.put("treatmentResults", treatmentResults);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfCureInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> bloodTypes = baseInfoMapper.getBloodTypes();
		baseInfo.put("bloodTypes", bloodTypes);
		
		List<Map<String, Object>> rhBloodTypes = baseInfoMapper.getRhBloodTypes();
		baseInfo.put("rhBloodTypes", rhBloodTypes);
		//
		List<Map<String, Object>> neonatalAdmissionTypes = baseInfoMapper.getNeonatalAdmissionTypes();
		baseInfo.put("neonatalAdmissionTypes", neonatalAdmissionTypes);
		
		List<Map<String, Object>> inpatientMedicalTreatments = baseInfoMapper.getInpatientMedicalTreatments();
		baseInfo.put("inpatientMedicalTreatments", inpatientMedicalTreatments);
		
		
		List<Map<String, Object>> intensiveCares  = baseInfoMapper.getStdIntensiveCare();
		baseInfo.put("intensiveCares", intensiveCares);
		List<Map<String, Object>> diagAccordTypes = baseInfoMapper.getDiagAccordTypes();
		baseInfo.put("diagAccordTypes", diagAccordTypes);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfOperateInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> incisionLevels = baseInfoMapper.getIncisionLevels();
		baseInfo.put("incisionLevels", incisionLevels);
		List<Map<String, Object>> cicatrizeTypes = baseInfoMapper.getCicatrizeTypes();
		baseInfo.put("cicatrizeTypes", cicatrizeTypes);
		List<Map<String, Object>> opsLevels = baseInfoMapper.getOpsLevels();
		baseInfo.put("opsLevels", opsLevels);
		
		List<Map<String, Object>> opsMarks = baseInfoMapper.getOpsMarks();
		baseInfo.put("opsMarks", opsMarks);
		
		List<Map<String, Object>> opsRiskLevels = baseInfoMapper.getOpsRiskLevels();
		baseInfo.put("opsRiskLevels", opsRiskLevels);
		
		List<Map<String, Object>> anaesthesiaWays = baseInfoMapper.getAnaesthesiaWays();
		baseInfo.put("anaesthesiaWays", anaesthesiaWays);
		
		
		List<Map<String, Object>> anaesthesiaLevels = baseInfoMapper.getAnaesthesiaLevels();
		baseInfo.put("anaesthesiaLevels", anaesthesiaLevels);
		
		List<Map<String, Object>> anaesthesiaUnintendeds = baseInfoMapper.getAnaesthesiaUnintendeds();
		baseInfo.put("anaesthesiaUnintendeds", anaesthesiaUnintendeds);
		
		List<Map<String, Object>> opsedComplications = baseInfoMapper.getOpsedComplications();
		baseInfo.put("opsedComplications", opsedComplications);
		
		List<Map<String, Object>> surgicalSiteInfections = baseInfoMapper.getSurgicalSiteInfections();
		baseInfo.put("surgicalSiteInfections", surgicalSiteInfections);
		
		List<Map<String, Object>> bodyParts = baseInfoMapper.getBaseInfoByTableName(TableNameEnum.BodyPart.getTableName());
		baseInfo.put("bodyParts", bodyParts);
		
		List<Map<String, Object>> patientSources = baseInfoMapper.getBaseInfoByTableName(TableNameEnum.PatientSource.getTableName());
		baseInfo.put("patientSources", patientSources);
		
		return baseInfo;
	}

	@Override
	public List<Map<String, Object>> getOperateByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getOperateByQueryName("%"+queryName+"%");
	}
	
	@Override
	public List<Map<String, Object>> getDRGsGroupByQueryName(String queryName) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getDRGsGroupByQueryName("%"+queryName+"%");
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfTransferDeptInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		medicalDepts = baseInfoMapper.getMedicalDeptByHospitalCode(hospitalCode);
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfInfusionBloodInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> bloodTypes = baseInfoMapper.getBaseInfoByTableName(TableNameEnum.BloodType.getTableName());
		baseInfo.put("bloodTypes", bloodTypes);
		//输血品种
		List<Map<String, Object>> bloodTransfusionVarieties = baseInfoMapper.getBloodTransfusionVarieties();
		baseInfo.put("bloodTransfusionVarieties", bloodTransfusionVarieties);
		//输血计量单位
		List<Map<String, Object>> bloodTransfusionMeasurementUnit = baseInfoMapper.getBloodTransfusionMeasurementUnit();
		baseInfo.put("bloodTransfusionMeasurementUnit", bloodTransfusionMeasurementUnit);
		
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfIntensiveCareInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> ICUTypes = baseInfoMapper.getICUTypes();
		baseInfo.put("ICUTypes", ICUTypes);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseDataOfDrugAllergyInfo() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> allergyDrugTypes = baseInfoMapper.getBaseInfoByTableName(TableNameEnum.DrugAllergyType.getTableName());
		baseInfo.put("allergyDrugTypes", allergyDrugTypes);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePagePressureSore() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> pressureSoreCradles = baseInfoMapper.getPressureSoreCradles();
		baseInfo.put("pressureSoreCradles", pressureSoreCradles);
		
		List<Map<String, Object>> pressureSorePhases = baseInfoMapper.getPressureSorePhases();
		baseInfo.put("pressureSorePhases", pressureSorePhases);
		
		List<Map<String, Object>> pressureSoreParts = baseInfoMapper.getPressureSoreParts();
		baseInfo.put("pressureSoreParts", pressureSoreParts);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageInfection() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> pathogens = baseInfoMapper.getPathogens();
		baseInfo.put("pathogens", pathogens);
		
		List<Map<String, Object>> pathogenyCheckSpecimens = baseInfoMapper.getPathogenyCheckSpecimens();
		baseInfo.put("pathogenyCheckSpecimens", pathogenyCheckSpecimens);
		
		List<Map<String, Object>> predisposeFactors = baseInfoMapper.getPredisposeFactors();
		baseInfo.put("predisposeFactors", predisposeFactors);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfHomePageAntibacterialDrug() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		
		List<Map<String, Object>> pathogens = baseInfoMapper.getPathogens();
		baseInfo.put("pathogens", pathogens);
		List<Map<String, Object>> pathogenyCheckSpecimens = baseInfoMapper.getPathogenyCheckSpecimens();
		baseInfo.put("pathogenyCheckSpecimens", pathogenyCheckSpecimens);
		
		List<Map<String, Object>> antibacterialDrugPurposes = baseInfoMapper.getAntibacterialDrugPurposes();
		baseInfo.put("antibacterialDrugPurposes", antibacterialDrugPurposes);
		List<Map<String, Object>> antibacterialDrugClassificatorys = baseInfoMapper.getAntibacterialDrugClassificatorys();
		baseInfo.put("antibacterialDrugClassificatorys",antibacterialDrugClassificatorys);
		
		List<Map<String, Object>> antibacterialDrugRegimens = baseInfoMapper.getAntibacterialDrugRegimens();
		baseInfo.put("antibacterialDrugRegimens", antibacterialDrugRegimens);
		
		return baseInfo;
	}

	@Override
	public List<Map<String, Object>> getMedicalDepts() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		medicalDepts = baseInfoMapper.getMedicalDeptByHospitalCode(hospitalCode);
		return medicalDepts;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfUnPigeonhole() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		
		medicalDepts = baseInfoMapper.getMedicalDeptByHospitalCode(hospitalCode);
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		List<Map<String, Object>> outHospitalTypes = baseInfoMapper.getOutHospitalTypes();
		baseInfo.put("outHospitalTypes", outHospitalTypes);
		
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfQualityControl() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> qualityControlPoints = baseInfoMapper.getQualityControlOfFirstLevel();
		baseInfo.put("qualityControlPoints", qualityControlPoints);
		return baseInfo;
	}

	/**
	 * 获取根目录指控标准
	 * @return
	 */
	public List<Map<String,Object>> getRootQualityControl(){
		return baseInfoMapper.getRootQualityControl();
	};
	
	/**
	 * 获取子获取指控标准
	 * @return
	 */
	public List<Map<String,Object>> getChildQualityControl(String code){
		return baseInfoMapper.getChildQualityControl(code);
	};
	
	@Override
	public List<Map<String, Object>> getQualityControlOfFirstLevel() {
		// TODO Auto-generated method stub
		return baseInfoMapper.getQualityControlOfFirstLevel();
	}

	@Override
	public List<Map<String, Object>> getQualityControlByUpOneLevelCode(String upOneLevelCode) {
		// TODO Auto-generated method stub
		return baseInfoMapper.getQualityControlByUpOneLevelCode(String.format("%s__", upOneLevelCode));
	}

	/**
	 * 依据质控描述获取质控项
	 * @param projectDesc
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getQualityControlOfLikeProject(String projectDesc){
		return baseInfoMapper.getQualityControlOfLikeProject(projectDesc);
	}
	
	
	/**
	 * 获取标签类型
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getMrPageTypes() {
		// TODO Auto-generated method stub
		return baseInfoMapper.getMrPageTypes();
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfPrint() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		medicalDepts = baseInfoMapper.getMedicalDeptByHospitalCode(hospitalCode);
		
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		List<Map<String, Object>> outHospitalTypes = baseInfoMapper.getOutHospitalTypes();
		baseInfo.put("outHospitalTypes", outHospitalTypes);
		List<Map<String, Object>> relativeRelations = baseInfoMapper.getRelativeRelations();
		baseInfo.put("relativeRelations", relativeRelations);
		List<Map<String, Object>> printerTypes = baseInfoMapper.getPrinterTypes();
		baseInfo.put("printerTypes", printerTypes);
		return baseInfo;
	}

	@Override
	public Map<String, List<Map<String, Object>>> getBaseInfoOfBorrow() {
		// TODO Auto-generated method stub
		Map<String, List<Map<String, Object>>> baseInfo = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String, Object>> hospitals = baseInfoMapper.getHospitalByCode(this.hospitalCode);
		List<Map<String, Object>> medicalDepts = new ArrayList<Map<String,Object>>();
		medicalDepts = baseInfoMapper.getMedicalDeptByHospitalCode(hospitalCode);
		
		baseInfo.put("hospitals", hospitals);
		baseInfo.put("medicalDepts", medicalDepts);
		List<Map<String, Object>> outHospitalTypes = baseInfoMapper.getOutHospitalTypes();
		baseInfo.put("outHospitalTypes", outHospitalTypes);
		List<Map<String, Object>> relativeRelations = baseInfoMapper.getRelativeRelations();
		baseInfo.put("relativeRelations", relativeRelations);
		List<Map<String, Object>> printerTypes = baseInfoMapper.getPrinterTypes();
		baseInfo.put("printerTypes", printerTypes);
		return baseInfo;
	}

	@Override
	public Map<String, Object> getSignatureMedicalWorks(List<String> medicalWorkCodes) {
		// TODO Auto-generated method stub
		Map<String,Object> paramMap = new HashMap<String, Object>();
		paramMap.put("list", medicalWorkCodes);
		Map<String, Object> medicalWorkerMap = new HashMap<String, Object>();
		List<Map<String, Object>> medicalWorkers = baseInfoMapper.getMedicalWorkerByWorkerCodes(paramMap);
		for (Map<String, Object> medicalWorker : medicalWorkers) {
			String code = (String)medicalWorker.get("uniq_code");
			String name = (String)medicalWorker.get("label");
			String signaturePath = StringUtil.meaningStr((String)medicalWorker.get("signature_path"));
			if(!medicalWorkerMap.containsKey(code) && signaturePath != null){
				Map<String, String> map = new HashMap<String, String>();
				map.put("code", code);
				map.put("name", name);
				map.put("signaturePath", myConfig.getWorkerSignaturePath()+ signaturePath);
				medicalWorkerMap.put(code, map);
			}
			
		}
		return medicalWorkerMap;
	}
}
